<?php

// 雪里开简易预约制访客管理系统 V1.0（2025.05.20）
// 演示地址: http://fangke.chalide.cn
// 更新关注: /weivote/yuyuefangke
// 文件路径: inc/sqls.php
// 文件大小: 3417 字节
// 最后修改时间: 2025-05-20 21:53:26
// 作者: yujianyue
// 邮件: 15058593138@qq.com
// 版权所有,保留发行权和署名权
/**
 * 数据库操作类
 * 功能反馈:15058593138@qq.com
 */

class MySQLDB {
    private $conn;
    
    // 构造函数，初始化数据库连接
    public function __construct() {
        $this->conn = db_conn();
    }
    
    // 析构函数，关闭数据库连接
    public function __destruct() {
        if ($this->conn) {
            mysqli_close($this->conn);
        }
    }
    
    // 执行SQL语句
    public function query($sql) {
        $result = mysqli_query($this->conn, $sql);
        if (!$result) {
            return false;
        }
        return $result;
    }
    
    // 获取单条记录
    public function getOne($sql) {
        $result = $this->query($sql);
        if (!$result) {
            return false;
        }
        $row = mysqli_fetch_assoc($result);
        mysqli_free_result($result);
        return $row;
    }
    
    // 获取多条记录
    public function getAll($sql) {
        $result = $this->query($sql);
        if (!$result) {
            return false;
        }
        $rows = [];
        while ($row = mysqli_fetch_assoc($result)) {
            $rows[] = $row;
        }
        mysqli_free_result($result);
        return $rows;
    }
    
    // 插入数据
    public function insert($table, $data) {
        $fields = [];
        $values = [];
        foreach ($data as $key => $value) {
            $fields[] = "`$key`";
            $values[] = "'" . $this->escape($value) . "'";
        }
        $field_str = implode(',', $fields);
        $value_str = implode(',', $values);
        
        $sql = "INSERT INTO `$table` ($field_str) VALUES ($value_str)";
        if ($this->query($sql)) {
            return mysqli_insert_id($this->conn);
        }
        return false;
    }
    
    // 更新数据
    public function update($table, $data, $where) {
        $sets = [];
        foreach ($data as $key => $value) {
            $sets[] = "`$key`='" . $this->escape($value) . "'";
        }
        $set_str = implode(',', $sets);
        
        $sql = "UPDATE `$table` SET $set_str WHERE $where";
        return $this->query($sql);
    }
    
    // 删除数据
    public function delete($table, $where) {
        $sql = "DELETE FROM `$table` WHERE $where";
        return $this->query($sql);
    }
    
    // 获取记录总数
    public function count($table, $where = '1=1') {
        $sql = "SELECT COUNT(*) as total FROM `$table` WHERE $where";
        $row = $this->getOne($sql);
        return $row ? $row['total'] : 0;
    }
    
    // 转义字符串
    public function escape($str) {
        return mysqli_real_escape_string($this->conn, $str);
    }
    
    // 获取带分页的数据列表
    public function getPageList($table, $where = '1=1', $order = 'id DESC', $page = 1, $pageSize = 10) {
        $start = ($page - 1) * $pageSize;
        
        $total = $this->count($table, $where);
        $totalPage = ceil($total / $pageSize);
        
        $sql = "SELECT * FROM `$table` WHERE $where ORDER BY $order LIMIT $start, $pageSize";
        $list = $this->getAll($sql);
        
        return [
            'list' => $list,
            'total' => $total,
            'totalPage' => $totalPage,
            'page' => $page,
            'pageSize' => $pageSize
        ];
    }
    
    // 获取错误信息
    public function getError() {
        return mysqli_error($this->conn);
    }
}
?>
